<?php
/**
 * (logic) 库存总表（SKU）
 */
namespace App\Logic\Inventory;

use App\Models\Inventory\InventoryTotalSku;
use App\Models\Product\ProductSku;
use Illuminate\Support\Facades\DB;

class SyncInventorySku{

    public static function exe(){

        $i = 0;
        ProductSku::query()
            ->with('inventory_cn')
            ->with('inventory_fba')
            ->with('os_box')
            ->with('os_removale')
            ->with('os_dropshipping')
            ->groupBy('product_sku.id')
            ->orderBy('product_sku.id')
            ->chunkById(500, function ($items) use (&$count,&$i){
                DB::beginTransaction();
                try{
                    foreach ($items as $item){
                        //国内库存
                        $totalCnInstant = $item->inventory_cn->sum('instant');
                        $totalCnTransit = $item->inventory_cn->sum('transit');
                        $totalCnReserve = $item->inventory_cn->sum('reserve');
                        //FBA库存
                        $totalFbaInstant = $item->inventory_fba->sum('quantity');
                        $totalFbaTransit = $item->inventory_fba->sum('inbound');
                        //海外仓库存（中转）
                        $totalOsBoxInstant = $item->os_box->sum('instant');
                        $totalOsBoxTransit = $item->os_box->sum('transit');
                        $totalOsBoxReserve = $item->os_box->sum('reserve');
                        //海外仓库存（移库）
                        $totalOsRemovaleInstant = $item->os_removale->sum('instant');
                        $totalOsRemovaleTransit = $item->os_removale->sum('transit');
                        $totalOsRemovaleReserve = $item->os_removale->sum('reserve');
                        //海外仓库存（代发）
                        $totalOsDropshippingInstant = $item->os_dropshipping->sum('instant');
                        $totalOsDropshippingTransit = $item->os_dropshipping->sum('transit');
                        $totalOsDropshippingReserve = $item->os_dropshipping->sum('reserve');

                        $flag1 = InventoryTotalSku::query()->updateOrCreate(
                            ['psku_id' => $item->id],
                            [
                                'classify_id' => $item->classify->id ?? 0,

                                'cn_instant' => $totalCnInstant,
                                'cn_transit' => $totalCnTransit,
                                'cn_reserve' => $totalCnReserve,

                                'fba_instant' => $totalFbaInstant,
                                'fba_transit' => $totalFbaTransit,

                                'os_box_instant' => $totalOsBoxInstant,
                                'os_box_transit' => $totalOsBoxTransit,
                                'os_box_reserve' => $totalOsBoxReserve,

                                'os_removale_instant' => $totalOsRemovaleInstant,
                                'os_removale_transit' => $totalOsRemovaleTransit,
                                'os_removale_reserve' => $totalOsRemovaleReserve,

                                'os_dropshipping_instant' => $totalOsDropshippingInstant,
                                'os_dropshipping_transit' => $totalOsDropshippingTransit,
                                'os_dropshipping_reserve' => $totalOsDropshippingReserve,
                            ]
                        );
                        if(!$flag1){
                            DB::rollBack();
                            continue;
                        }
                        echo '......'.$i++.'......'.PHP_EOL;
                    }
                    DB::commit();
                }catch (\Exception $e){
                    DB::rollBack();
                    report($e);
                }
            });

    }


}
